import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import React, { useEffect, useRef, useState } from 'react';
import moment from 'moment';
import { getFlowOrderExcel } from './service';
import { fetchChannelUser } from '../DyCps/service';
import { useModel } from '@umijs/max';
import buildQueryString from '@/utils/buildQueryString';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();
  const [dateRange, setDateRange] = useState<any>();
  const { initialState } = useModel('@@initialState');
    const { currentUser } = initialState || {};
    const [requestStatus] = useState(currentUser?.userInfo?.role!=='super')
  /**
   * @en-US International configuration
   * @zh-CN 国际化配置
   * */
  useEffect(() => {
    const startOfDay = new Date(new Date());
    startOfDay.setHours(0, 0, 0, 0);
    const endOfDay = new Date(new Date).setHours(23,59,59,0);
    setDateRange([startOfDay, endOfDay]);
  }, []);
  const columns: ProColumns<any>[] = [
    {
      title: '跟单创建时间',
      dataIndex: 'createTime',
      valueType: 'dateTime',
      hideInSearch: true,
    },
    {
      title: '渠道',
      dataIndex: 'uid',
      valueType: "select",
      hideInSearch: requestStatus,
      hideInTable: requestStatus,
      request: async () => {
        const data = await fetchChannelUser({});
        return data.data
      }
    },
    {
      title: '文件名',
      dataIndex: 'title',
      copyable: true,
      hideInSearch: true,
    },
    {
      title: '下载链接',
      dataIndex: 'url',
      copyable: true,
      hideInSearch: true,
    },
   
    {
      title: '创建跟单时间',
      dataIndex: 'createTime',
      valueType: 'dateRange',
      hideInForm: true,
      hideInTable: true,
      search: {
        transform: (value) => {
          return {
            startTime: value[0],
            endTime: value[1],
          };
        },
        fieldProps: {
          value: dateRange,
          onChange: (value:any) => {
            if (value && value.length === 2) {
              setDateRange(value);
            }
          },
        },
      },
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      render: (text, record) => [
        <a
          key="editable"
          onClick={async () => {
            let params = {
              title: record.title,
              token: localStorage.getItem('token'),
              exportType: 'download'
            }
            let path = '/api/exportDyFile' + buildQueryString(params);
            window.open(path);
          }}
        >
          导出
        </a> ,
        <a
          key="editable"
          onClick={async () => {
           window.open(record?.url);
          }}
        >
          下载
        </a>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<API.RuleListItem, API.PageParams>
        headerTitle={'全部跟单'}
        actionRef={actionRef}
        rowKey="key"
        request={async () => {
          const startTime = moment(new Date(dateRange[0])).format('YYYY-MM-DD HH:mm:ss');
          const endTime = moment(new Date(dateRange[1]).setHours(23,59,59,0)).format('YYYY-MM-DD HH:mm:ss');
          // 将默认值合并到请求参数中
          const data = { startTime, endTime };
          
          const rsp = await getFlowOrderExcel(data);
          if (rsp.code === 'SUCCESS') {
            return {
              data: rsp.data,
            };
          }
        }}
        search={{
          labelWidth: 100,
        }}
        columns={columns}
      />
      
    </PageContainer>
  );
};
export default TableList;
